/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sessions.service;

import java.util.List;

import com.jeeplus.common.utils.IdGen;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.mapper.MemberMapper;
import com.jeeplus.modules.partitioninfo.entity.PartitionInfo;
import com.jeeplus.modules.partitioninfo.mapper.PartitionInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sessions.entity.Sessions;
import com.jeeplus.modules.sessions.mapper.SessionsMapper;

/**
 * 抽奖场次Service
 * @author w
 * @version 2021-01-25
 */
@Service
@Transactional(readOnly = true)
public class SessionsService extends CrudService<SessionsMapper, Sessions> {

	@Autowired
	private MemberMapper memberMapper;
	@Autowired
	private PartitionInfoMapper partitionInfoMapper;

	public Sessions get(String id) {
		return super.get(id);
	}
	
	public List<Sessions> findList(Sessions sessions) {
		return super.findList(sessions);
	}
	
	public Page<Sessions> findPage(Page<Sessions> page, Sessions sessions) {
		return super.findPage(page, sessions);
	}
	
	@Transactional(readOnly = false)
	public void save(Sessions sessions) {
		if (sessions.getIsNewRecord()){
			super.save(sessions);
			//如果添加新的场次，给当前用户配置
			List<Member> members = memberMapper.findAllList(new Member());
			for (Member member : members) {
				if (!"admin".equals(member.getLoginName())){
					PartitionInfo partitionInfo = new PartitionInfo();
					partitionInfo.setId(IdGen.uuid());
					partitionInfo.setMember(member);
					partitionInfo.setSessions(sessions);
					partitionInfo.setEnergyValue(10);
					partitionInfo.setStars(0);
					partitionInfo.setIsNewRecord(true);
					partitionInfo.preInsert();
					partitionInfoMapper.insert(partitionInfo);
				}
			}
		}else {
			super.save(sessions);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Sessions sessions) {
		super.delete(sessions);
	}
	
}